<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'分红明细'),
	array('op'=>'listday','title'=>'每日分红统计'),
	array('op'=>'listmonth','title'=>'每月分红统计'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('commission',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'address','title'=>'代理地区','type'=>'string'),
			array('name'=>'money','title'=>'分红','type'=>'string'),
			array('name'=>'level','title'=>'分红类型','type'=>'radio','options'=>'commission_money_types'),
			array('name'=>'xuni_money','title'=>$this->xuniname,'type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'status','title'=>'分红状态','type'=>'radio','options'=>'commission_status'),
			
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'detail','title'=>'分红详情'),
			array('type'=>'modal','op'=>'change','title'=>'修改佣金'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'choose','width'=>18,'action'=>'jy_merch/manage.get_member/','name'=>'mid','title'=>'选择会员'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'分红状态','options'=>'commission_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['commission_status']=$this->commission_status;
	$return['commission_money_types']=$this->commission_money_types;
	exi($return);
}
if($_RQ['op']=='listday'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$daynum=date("t",strtotime($y.'-'.$m));
	$list=array();
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_mareafee_commission',$params,'money');
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'fee'=>$fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'日期','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$params=array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	$list=array();
	for($i=1;$i<=12;$i++){
		$starttime=strtotime($y.'-'.$i);
		if($i==12){
			$ny=$y+1;
			$endtime=strtotime($ny.'-01');
		}else{
			$endtime=strtotime($y.'-'.$m+1);
		}
		$params['createtime between']=array($starttime,$endtime);
		$fee=pdo_getsum('jy_mareafee_commission',$params,'money');
		$list[]=array(
			'title'=>$y.'-'.$i,
			'fee'=>$fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'月份','type'=>'string'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	exi($return);
}
if($_RQ['op']=='detail'){
	$item=$this->get('commission',array('id'=>intval($_RQ['id'])),array('id','remark'));
	if(!$item){
		exi('指定分红不存在！','error');
	}
	$item['remark']=$this->model('commission')->commission_remark($item['remark']);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'分红明细',
		'look'=>array(
			array('type'=>'text','name'=>'remark','title'=>'分红明细'),
		),
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('commission',array('money'=>$post['money']),array('id'=>$id));
		exi('修改分红成功！','','list');
	}
	$item=$this->get('commission',array('id'=>intval($_RQ['id'])),array('id','money'));
	if(!$item){
		exi('指定分红不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改分红',
		'edit'=>array(
			array('type'=>'string','name'=>'money','title'=>'设置分红','help'=>'分红异常时操作'),
		),
	);
	exi($return);
}